package cn.zz.camerasdk.utils;

/**
 * author: zhu on 2017/7/7 17:09
 * email: mackkill@gmail.com
 */


import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    public ZipUtils() {
    }

    public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception {
        ArrayList fileList = new ArrayList();
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
        String szName = "";

        ZipEntry zipEntry;
        while((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            File file;
            if(zipEntry.isDirectory()) {
                szName = szName.substring(0, szName.length() - 1);
                file = new File(szName);
                if(bContainFolder) {
                    fileList.add(file);
                }
            } else {
                file = new File(szName);
                if(bContainFile) {
                    fileList.add(file);
                }
            }
        }

        inZip.close();
        return fileList;
    }

    public static InputStream UpZip(String zipFileString, String fileString) throws Exception {
        ZipFile zipFile = new ZipFile(zipFileString);
        ZipEntry zipEntry = zipFile.getEntry(fileString);
        return zipFile.getInputStream(zipEntry);
    }

    public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
        String szName = "";

        while(true) {
            ZipEntry zipEntry;
            while((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                File file;
                if(zipEntry.isDirectory()) {
                    szName = szName.substring(0, szName.length() - 1);
                    file = new File(outPathString + File.separator + szName);
                    file.mkdirs();
                } else {
                    file = new File(outPathString + File.separator + szName);
                    if(!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }

                    file.createNewFile();
                    FileOutputStream out = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];

                    int len;
                    while((len = inZip.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                        out.flush();
                    }

                    out.close();
                }
            }

            inZip.close();
            return;
        }
    }

    public static void ZipFolder(String srcFileString, String zipFileString) throws Exception {
        Log.v("XZip", "ZipFolder(String, String)");
        ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
        File file = new File(srcFileString);
        ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
        outZip.finish();
        outZip.close();
    }

    private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception {
        if(zipOutputSteam != null) {
            File file = new File(folderString + fileString);
            if(file.isFile()) {
                ZipEntry fileList = new ZipEntry(fileString);
                FileInputStream i = new FileInputStream(file);
                zipOutputSteam.putNextEntry(fileList);
                byte[] buffer = new byte[4096];

                int len;
                while((len = i.read(buffer)) != -1) {
                    zipOutputSteam.write(buffer, 0, len);
                }

                zipOutputSteam.closeEntry();
            } else {
                String[] var8 = file.list();
                if(var8.length <= 0) {
                    ZipEntry var9 = new ZipEntry(fileString + File.separator);
                    zipOutputSteam.putNextEntry(var9);
                    zipOutputSteam.closeEntry();
                }

                for(int var10 = 0; var10 < var8.length; ++var10) {
                    ZipFiles(folderString, fileString + File.separator + var8[var10], zipOutputSteam);
                }
            }

        }
    }
}